<template>
  <p>
    <button @click="add">
      <i class="ace-icon fa fa-edit"></i>
      新增
    </button>
    &nbsp;
    <button @click="refresh">
      <i class="ace-icon fa fa-undo"></i>
      刷新
    </button>
  </p>
  <table id="simple-table" class="table  table-bordered table-hover">
    <thead>
      <tr>
          <th>id</th>
          <th>手机号</th>
          <th>密码</th>
          <th>昵称</th>
          <th>头像url</th>
          <th>注册时间</th>
          <th>操作</th>
      </tr>
    </thead>

    <tbody>
    <tr v-for="member in members" :key="member.id">
          <td>{{ member.id }}</td>
          <td>{{ member.mobile }}</td>
          <td>{{ member.password }}</td>
          <td>{{ member.name }}</td>
          <td>{{ member.photo }}</td>
          <td>{{ member.registerTime }}</td>
      <td>
        <div class="hidden-sm hidden-xs btn-group">

          <button @click="edit(member)" class="btn btn-xs btn-info">
            <i class="ace-icon fa fa-pencil bigger-120"></i>
          </button>

          <button @click="del(member.id)" class="btn btn-xs btn-danger">
            <i class="ace-icon fa fa-trash-o bigger-120"></i>
          </button>
        </div>

        <div class="hidden-md hidden-lg">
          <div class="inline pos-rel">
            <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
              <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
            </button>

            <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
              <li>
                <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                </a>
              </li>

              <li>
                <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                </a>
              </li>

              <li>
                <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                </a>
              </li>
            </ul>
          </div>
        </div>
      </td>
    </tr>
    </tbody>
  </table>
  <!-- 分页插件 -->
  <pagination ref="paginationRef" :list="getMemberList" />

  <!-- Modal -->
  <div class="modal fade" id="form-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">表单</h4>
        </div>
        <div class="modal-body">
          <form class="form-horizontal">
                 <div class="form-group">
                   <label class="col-sm-2 control-label">手机号</label>
                   <div class="col-sm-10">
                   <input v-model="member.mobile" type="text" class="form-control">
                </div>
              </div>
                 <div class="form-group">
                   <label class="col-sm-2 control-label">密码</label>
                   <div class="col-sm-10">
                   <input v-model="member.password" type="text" class="form-control">
                </div>
              </div>
                 <div class="form-group">
                   <label class="col-sm-2 control-label">昵称</label>
                   <div class="col-sm-10">
                   <input v-model="member.name" type="text" class="form-control">
                </div>
              </div>
                 <div class="form-group">
                   <label class="col-sm-2 control-label">头像url</label>
                   <div class="col-sm-10">
                   <input v-model="member.photo" type="text" class="form-control">
                </div>
              </div>
                 <div class="form-group">
                   <label class="col-sm-2 control-label">注册时间</label>
                   <div class="col-sm-10">
                   <input v-model="member.registerTime" type="text" class="form-control">
                </div>
              </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
          <button @click="saveMember" type="button" class="btn btn-primary">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import {onMounted, ref} from "vue";
import pagination from "@/components/pagination";

export default {
  name: "Member",
  components: {
    pagination,
  },
  setup() {
    // members 数组
    const members = ref([]);
    const member = ref({}); // 保存会员表单
    const paginationRef = ref(null);

    // 枚举字段

    // 刷新
    const refresh = () => {
      getMemberList(1);
    }

    // 获取会员列表
    const getMemberList = (page) => {
      Loading.show();
      axios.post(process.env.VUE_APP_SERVER + "/business/admin/member/list",
          {
            page: page,
            size: paginationRef.value.size
          }
      ).then((response) => {
        Loading.hide();
        const resp = response.data;
        members.value = resp.content.list;
        paginationRef.value.render(page, resp.content.total);
      })
    }

    // 保存会员
    const saveMember = () => {
      // 保存参数校验
      if ( 1 != 1
                || !Validator.require(member.value.id, "id")
                || !Validator.require(member.value.mobile, "手机号")
                || !Validator.length(member.value.mobile, "手机号", 1, 11)
                || !Validator.require(member.value.password, "密码")
                || !Validator.length(member.value.name, "昵称", 1, 50)
                || !Validator.length(member.value.photo, "头像url", 1, 200)
        )
      {
        return;
      }

      Loading.show();
      axios.post(process.env.VUE_APP_SERVER + "/business/admin/member/save", member.value).then((response) => {
        const resp = response.data;
        if (resp.success) {
          Loading.hide();
          $('#form-modal').modal('hide');
          Toast.success("保存成功");
          getMemberList(1);
        } else {
          Loading.hide();
          $('#form-modal').modal('hide');
          Toast.warning(resp.message);
        }
      })
    }

    // 删除会员
    const del = (id) => {
      Confirm.show("删除后不可恢复，确认删除？", function () {
        Loading.show();
        axios.delete(process.env.VUE_APP_SERVER + "/business/admin/member/delete/" + id).then((response) => {
          const resp = response.data;
          if (resp.success) {
            //$('#form-modal').modal('hide');
            getMemberList(1);
          }
        })
        Toast.success("删除成功");
      })
    }

    // 添加会员
    const add = () => {
      member.value = {};
      $('#form-modal').modal('show');
    }

    // 修改会员
    const edit = (param) => {
      // 方法一
      //member.value = JSON.parse(JSON.stringify(param));
      // 方法二
      member.value = $.extend({}, param);
      $('#form-modal').modal('show');
    }


    // 初始加载第一页
    onMounted(() => {
      getMemberList(1);
    })

    return {
      members,
      refresh,
      paginationRef,
      getMemberList,
      add,
      saveMember,
      member,
      edit,
      del,
    }
  }
}
</script>

<style scoped>

</style>